package com.ruike.alisurface.ui.answerQuestion;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.ui.answerQuestion.bean.AnswerQuestionBean;
import com.ruike.alisurface.ui.answerQuestion.http.TongNuoHttp;
import com.ruike.alisurface.ui.mainPage.MainActivity;
import com.ruike.alisurface.ui.mainPage.MainTypeActivity;
import com.ruike.alisurface.utils.ShareKey;
import com.ruike.alisurface.utils.ZXingUtils;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.ShareUtils;
import com.voodoo.lib_utils.imageLoader.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: voodoo
 * CreateDate: 2023/11/20 020上午 11:08
 * Description: 二维码界面
 */
public class QrCodeActivity extends BaseAty {

    @BindView(R.id.qrCode_goBack_imgv)
    ImageView goBackImgv;
    @BindView(R.id.qrCode_qrcode_img)
    ImageView qrCodeImg;
    @BindView(R.id.qrCode_countdownTime_tv)
    TextView countdownTimeTv;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qr_code;
    }

    @Override
    protected void initViews() {
        boolean isAdv = ShareUtils.getInstance().getInt(ShareKey.ADV_TYPE, Constant.ADV_TYPE_NONE) == Constant.ADV_TYPE_NONE;
        initTitleBar(isAdv);
        goBackImgv.setVisibility(isAdv ? View.GONE : View.VISIBLE);
        if (!isAdv) {
            startCountdownTimer(60, countdownTimeTv, null);
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        showProgressDialog("正在获取二维码数据...");
        TongNuoHttp.checkCanUse(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.qrCode_goBack_imgv:
                startCountdownTimer(0, null, null);
                break;
        }
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
        if ("答题扫码登录成功".equals(event.type)) {
//            startActivity(AnswerQuestionActivity.class, null);
            startActivity(QuestionProductListActivity.class, null);
            finish();
        }
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        // 检查机器是否可用，并返回二维码内容
        if (TongNuoHttp.CHECK_CAN_USE_URL.equals(requestUrl)) {
            String qrCodeUrlStr = "未获取到二维码内容";
            try {
                JSONObject qrCodeJSON = new JSONObject(requestJsonStr);
                JSONObject dataJson = qrCodeJSON.optJSONObject("data");
                if (dataJson != null) {
                    qrCodeUrlStr = dataJson.optString("qrcode_url");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            // 生成二维码并展示到界面上
            Bitmap qrImageBitmap = ZXingUtils.getInstance().createQRImage(qrCodeUrlStr, 300, 300, null);
            ImageLoader.loadImage(this, qrImageBitmap, qrCodeImg);
            // 然后就是等待Mqtt的回传
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
    }

}
